package org.ediba.siri.web.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ediba.siri.core.user.domain.User;
import org.ediba.siri.core.user.domain.UserGroup;
import org.ediba.siri.core.user.domain.UserRole;
import org.ediba.siri.core.user.service.UserService;
import org.json.JSONException;
import org.springframework.web.bind.annotation.ResponseBody;

public class UserGroupController extends Controller {

	private UserService userService;
	public void setUserService(UserService userService) {this.userService = userService;}
	
	@ResponseBody
	public Map<String,Object> getHandler(HttpServletRequest request, HttpServletResponse response, UserGroupCommand command){
		
		List<UserGroup> groups = null;
		if (command.getIdsUser() != null)
			groups = this.userService.groupRetrieveByUser(command.getIdsUser()[0]);
		else 
			groups = this.userService.groupRetrieve();
		
		Map<String,Object> _result = new HashMap<String,Object>(4);
		_result.put("data", groups);
		_result.put("total", groups.size());
		_result.put("success", true);
		_result.put("message", "OK");
		return _result;
	}

	@ResponseBody
	public Map<String,Object> postHandler(HttpServletRequest request, HttpServletResponse response, UserGroupCommand command){
		
		String name = null;
		if (command.getData() != null)
			try {name = command.getData().getString("name");
			} catch (JSONException e) {e.printStackTrace();}
		
		UserGroup userGroup = this.userService.groupCreate(name);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("data", userGroup);
		_result.put("success", userGroup!=null);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> putHandler(HttpServletRequest request, HttpServletResponse response, UserGroupCommand command){
		
		Integer id = null;
		String name = null;
		if (command.getData() != null)
			try {
				id = command.getData().getInt("id");
				name = command.getData().getString("name");
			} catch (JSONException e) {e.printStackTrace();}
		else{
			id = command.getId();
			name = command.getName();
		}
		
		UserGroup userGroup = this.userService.groupRetrieve(id);
		if (name!=null)
			userGroup.setName(name);
		userGroup = this.userService.groupUpdate(userGroup);

		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", true);
		_result.put("data", userGroup);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> deleteHandler(HttpServletRequest request, HttpServletResponse response, UserGroupCommand command){

		Integer id = null;
		if (command.getData() != null)
			try {id = command.getData().getInt("id");
			} catch (JSONException e) {e.printStackTrace();}
		
		boolean _success = this.userService.groupDelete(id);
		
		Map<String,Object> _result = new HashMap<String,Object>(1);
		_result.put("success", _success);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> attachUserRoleHandler(HttpServletRequest request, HttpServletResponse response, UserGroupCommand command){
				
		UserGroup userGroup = this.userService.groupRetrieve(command.getId());
		UserRole userRole = this.userService.roleRetrieve(command.getIdsUserRole()[0]);
		
		userGroup.getUserroles().add(userRole);
		userGroup = this.userService.groupUpdate(userGroup);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", true);
		_result.put("data", userGroup);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> deattachUserRoleHandler(HttpServletRequest request, HttpServletResponse response, UserGroupCommand command){
		
		UserGroup userGroup = this.userService.groupRetrieve(command.getId());
		UserRole userRole = this.userService.roleRetrieve(command.getIdsUserRole()[0]);

		userGroup.getUserroles().remove(userRole);
		userGroup = this.userService.groupUpdate(userGroup);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", true);
		_result.put("data", userGroup);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> attachUserHandler(HttpServletRequest request, HttpServletResponse response, UserGroupCommand command){

		User user = this.userService.retrieve(command.getIdsUser()[0]);
		UserGroup userGroup = this.userService.groupRetrieve(command.getId());
		
		user.getUsergroups().add(userGroup);
		user = this.userService.update(user);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", true);
		_result.put("data", user);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> deattachUserHandler(HttpServletRequest request, HttpServletResponse response, UserGroupCommand command){
		
		User user = this.userService.retrieve(command.getIdsUser()[0]);
		UserGroup userGroup = this.userService.groupRetrieve(command.getId());
		
		user.getUsergroups().remove(userGroup);
		user = this.userService.update(user);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", true);
		_result.put("data", user);
		return _result;
	}
}
